/**
 * 
 */
package com.rio.dandan.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.rio.dandan.bean.PageBean;
import com.rio.dandan.dao.UserMailDao;
import com.rio.dandan.dao.UserProfileDao;
import com.rio.dandan.exception.AccountNoPermitException;
import com.rio.dandan.pojo.UserMail;
import com.rio.dandan.pojo.UserProfile;
import com.rio.dandan.service.UserMailService;
import com.rio.dandan.util.SecurityUtils;

/**
 * 
 * UserPhotoServiceImpl.java
 * 
 * @author 宋陆
 * @version 1.0 2014年5月22日
 */
@Service
public class UserMailServiceImpl implements UserMailService {

    @Resource
    private UserMailDao userMailDao;
    @Resource
    private UserProfileDao userProfileDao;

    /**
     * @see com.rio.dandan.service.UserMailService#findUserSendMail(com.rio.dandan.bean.PageBean)
     */
    @Override
    public PageBean<UserMail> findUserSendMail(PageBean<UserMail> pageBean) {
        String sendUid = SecurityUtils.getCurrUId();
        userMailDao.findUserSendMail(pageBean, sendUid);
        return pageBean;
    }

    /**
     * @see com.rio.dandan.service.UserMailService#findUserReceiveMail(com.rio.dandan.bean.PageBean)
     */
    @Override
    public PageBean<UserMail> findUserReceiveMail(PageBean<UserMail> pageBean) {
        String receiveUid = SecurityUtils.getCurrUId();
        userMailDao.findUserReceiveMail(pageBean, receiveUid);
        List<UserMail> mails = pageBean.getRows();
        if (mails.size() == 0) {
            return pageBean;
        }
        List<String> idList = new ArrayList<String>();
        for (UserMail userMail : mails) {
            idList.add(userMail.getSendUid());
        }
        List<UserProfile> userProfileList = userProfileDao.findUserProfile(idList.toArray(new String[] {}));
        Map<String, UserProfile> userProfileMap = new HashMap<String, UserProfile>();
        for (UserProfile userProfile : userProfileList) {
            userProfileMap.put(userProfile.getUid(), userProfile);
        }
        for (UserMail userMail : mails) {
            if (userProfileMap.containsKey(userMail.getSendUid())) {
                userMail.setSender(userProfileMap.get(userMail.getSendUid()));
            }
        }
        return pageBean;
    }

    /**
     * @see com.rio.dandan.service.UserMailService#saveOrUpdateUserMail(com.rio.dandan.pojo.UserMail)
     */
    @Override
    public UserMail saveOrUpdateUserMail(UserMail userMail) {
        return userMailDao.saveOrUpdateUserMail(userMail);
    }

    /**
     * @see com.rio.dandan.service.UserMailService#viewUserMail(java.lang.String)
     */
    @Override
    public UserMail viewUserMail(String mailId) {
        // 检查权限如果接收人或发送人不对，抛出无权限异常
        UserMail userMail = userMailDao.getUserMail(mailId);
        String uid = SecurityUtils.getCurrUId();
        if (!uid.equals(userMail.getSendUid()) && !uid.equals(userMail.getReceiveUid())) {
            throw new AccountNoPermitException();
        }
        userMail.setReceiveStatus((short) -1);
        userMailDao.saveOrUpdateUserMail(userMail);
        return userMail;
    }

    /**
     * @see com.rio.dandan.service.UserMailService#deleteUserMail(java.lang.String)
     */
    @Override
    public UserMail deleteUserMail(String mailId) {
        UserMail userMail = userMailDao.getUserMail(mailId);
        String uid = SecurityUtils.getCurrUId();
        if (!uid.equals(userMail.getSendUid()) && !uid.equals(userMail.getReceiveUid())) {
            throw new AccountNoPermitException();
        }
        if (uid.equals(userMail.getSendUid())) {
            userMail.setSendStatus((short) -1);
        }
        if (uid.equals(userMail.getReceiveUid())) {
            userMail.setReceiveStatus((short) -1);
        }
        userMailDao.saveOrUpdateUserMail(userMail);
        return userMail;
    }

}
